package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.IEntity;
import com.dianping.cat.alarm.rule.IVisitor;
import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
























public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  46 */   public DefaultXmlBuilder() { this(false); }



/*  50 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  54 */     this.m_compact = compact;
/*  55 */     this.m_sb = sb;
/*  56 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  60 */     entity.accept(this.m_visitor);
/*  61 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  65 */     this.m_level--;

/*  67 */     indent();
/*  68 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  72 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  76 */     if (value == null) {
/*  77 */       return null;
    }

/*  80 */     String str = toString(value);
/*  81 */     int len = str.length();
/*  82 */     StringBuilder sb = new StringBuilder(len + 16);

/*  84 */     for (int i = 0; i < len; i++) {
/*  85 */       char ch = str.charAt(i);

/*  87 */       switch (ch) {
        case '<':
/*  89 */           sb.append("&lt;");
          break;
        case '>':
/*  92 */           sb.append("&gt;");
          break;
        case '&':
/*  95 */           sb.append("&amp;");
          break;
        case '"':
/*  98 */           if (!text) {
/*  99 */             sb.append("&quot;");
            break;
          }
        default:
/* 103 */           sb.append(ch);
          break;
      }

    }
/* 108 */     return sb.toString();
  }

  protected void indent() {
/* 112 */     if (!this.m_compact) {
/* 113 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 114 */         this.m_sb.append("   ");
      }
    }
  }


/* 120 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 124 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 128 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 132 */     indent();

/* 134 */     this.m_sb.append('<').append(name);

/* 136 */     int len = nameValues.length;

/* 138 */     for (int i = 0; i + 1 < len; i += 2) {
/* 139 */       Object attrName = nameValues[i];
/* 140 */       Object attrValue = nameValues[i + 1];

/* 142 */       if (attrValue != null) {
/* 143 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 147 */     if (dynamicAttributes != null) {
/* 148 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 149 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 153 */     if (text != null && closed) {
/* 154 */       this.m_sb.append('>');
/* 155 */       this.m_sb.append(escape(text, true));
/* 156 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 158 */       if (closed) {
/* 159 */         this.m_sb.append('/');
      } else {
/* 161 */         this.m_level++;
      }

/* 164 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 170 */     if (value instanceof String)
/* 171 */       return (String)value; 
/* 172 */     if (value instanceof Collection) {
/* 173 */       Collection<Object> list = (Collection)value;
/* 174 */       StringBuilder sb = new StringBuilder(32);
/* 175 */       boolean first = true;

/* 177 */       for (Object item : list) {
/* 178 */         if (first) {
/* 179 */           first = false;
        } else {
/* 181 */           sb.append(',');
        }

/* 184 */         if (item != null) {
/* 185 */           sb.append(item);
        }
      }

/* 189 */       return sb.toString();
/* 190 */     }  if (value.getClass().isArray()) {
/* 191 */       int len = Array.getLength(value);
/* 192 */       StringBuilder sb = new StringBuilder(32);
/* 193 */       boolean first = true;

/* 195 */       for (int i = 0; i < len; i++) {
/* 196 */         Object item = Array.get(value, i);

/* 198 */         if (first) {
/* 199 */           first = false;
        } else {
/* 201 */           sb.append(',');
        }

/* 204 */         if (item != null) {
/* 205 */           sb.append(item);
        }
      }

/* 209 */       return sb.toString();
    }

/* 212 */     return String.valueOf(value);
  }


  public void visitCondition(Condition condition) {
/* 217 */     startTag("condition", null, new Object[] { "minute", condition.getMinute(), "title", condition.getTitle(), "alertType", condition.getAlertType() });

/* 219 */     if (!condition.getSubConditions().isEmpty()) {
/* 220 */       for (SubCondition subCondition : condition.getSubConditions()) {
/* 221 */         subCondition.accept(this.m_visitor);
      }
    }

/* 225 */     endTag("condition");
  }


  public void visitConfig(Config config) {
/* 230 */     startTag("config", null, new Object[] { "starttime", config.getStarttime(), "endtime", config.getEndtime() });

/* 232 */     if (!config.getConditions().isEmpty()) {
/* 233 */       for (Condition condition : config.getConditions()) {
/* 234 */         condition.accept(this.m_visitor);
      }
    }

/* 238 */     endTag("config");
  }



/* 243 */   public void visitMetricItem(MetricItem metricItem) { startTag("metric-item", true, null, new Object[] { "monitorCount", metricItem.getMonitorCount(), "monitorSum", metricItem.getMonitorSum(), "monitorAvg", metricItem.getMonitorAvg(), "metricItemText", metricItem.getMetricItemText(), "productText", metricItem.getProductText() }); }



  public void visitMonitorRules(MonitorRules monitorRules) {
/* 248 */     startTag("monitor-rules", null, new Object[0]);

/* 250 */     if (!monitorRules.getRules().isEmpty()) {
/* 251 */       for (Rule rule : monitorRules.getRules().values()) {
/* 252 */         rule.accept(this.m_visitor);
      }
    }

/* 256 */     endTag("monitor-rules");
  }


  public void visitRule(Rule rule) {
/* 261 */     startTag("rule", rule.getDynamicAttributes(), new Object[] { "id", rule.getId() });

/* 263 */     if (!rule.getMetricItems().isEmpty()) {
/* 264 */       for (MetricItem metricItem : rule.getMetricItems()) {
/* 265 */         metricItem.accept(this.m_visitor);
      }
    }

/* 269 */     if (!rule.getConfigs().isEmpty()) {
/* 270 */       for (Config config : rule.getConfigs()) {
/* 271 */         config.accept(this.m_visitor);
      }
    }

/* 275 */     endTag("rule");
  }



/* 280 */   public void visitSubCondition(SubCondition subCondition) { startTag("sub-condition", true, null, new Object[] { "type", subCondition.getType(), "text", subCondition.getText() }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */